import HttpUtils from '../HttpUtils/HttpUtils'; // 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;

Page({
	...HttpUtils.func,
	data: {
		titOn: 'all',
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		status: '',
		partyVoteList: []
	},
	handleTap1(event) { //会议筛选
		var sessType = event.target.id;
		var going;
		var pass;
		var abence;
		if (sessType == "going") {
			going = "goingShow";
			pass = "passHide";
			abence = "abenceHide";
		} else if (sessType == "pass") {
			pass = "passShow";
			going = "goingHide";
			abence = "abenceHide";
		} else if (sessType == "abence") {
			abence = "abenceShow";
			going = "goingHide";
			pass = "passHide";
		} else if (sessType == "all") {
			abence = "abenceShow";
			going = "goingShow";
			pass = "passShow";
		}
		this.setData({
			titOn: sessType,
			going: going,
			pass: pass,
			abence: abence

		})
	},
	onLoad() {
		//投票列表查询接口
		this.loadpartyVoteList(this.data.status);
	},


	//投票列表查询接口
	loadpartyVoteList: function (e) {
		var status = 0;
		if ("string" == typeof (e)) {
			status = e;
		} else {
			if (e != undefined) {
				status = e.currentTarget.dataset.code; //表示从页面上点击过来de,是一个对象
				this.handleTap1(e)
				this.setData({
					"pageIndex": 1,
					"status": status
				})
			} else {
				status = this.data.status; //表示从页面上拉加载的 直接获取
			}
		}
		var newUrl = getApp().globalData.dangjianUrl + "/vote/partyVoteList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "投票列表查询接口调用失败").then(
			(respData) => {
				console.log(respData)
				var result = respData.data.result.code;
				if (1 == result) {
					var votes = respData.data.vote;
					var totalPages = votes.totalPages;
					var pageIndex = votes.pageIndex;
					var items = votes.items;
					var className;
					var statusName;
					votes = votes.voteResponseList;
					var partyVoteList = [];
					for (var index in votes) {
						var vote = votes[index];
						var json = {};
						json["voteId"] = vote.voteId;
						json["title"] = vote.title;
						json["subject"] = vote.subject;
						json["startDate"] = this.formatDate('yyyy.MM.dd', vote.startDate);
						json["endDate"] = this.formatDate('yyyy.MM.dd', vote.endDate);
						json["status"] = vote.status;
						json["voteType"] = vote.voteType;
						json["voteCount"] = vote.voteCount;
						json["userCount"] = vote.userCount;
						json["subject"] = vote.subject;
						json["title"] = vote.title;
						json["voteStatus"] = vote.voteUsers[0].voteStatus;
						partyVoteList.push(json);
					}
					console.log(partyVoteList)
					if (e == undefined) {
						var oldpartyVoteList = this.data.partyVoteList;
						partyVoteList = oldpartyVoteList.concat(partyVoteList); //表示从页面上拉加载的 直接获取
					}
					this.setData({
						"partyVoteList": partyVoteList,
						"totalPages": totalPages,
						"pageIndex": pageIndex,
						"pageitems": items,
					});
				} else {
					this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	sessionDetail(event) { //会议详情
		var sessType = event.target.dataset.type;
		var pageUrl = "../sessionDetail/sessionDetail?sessType=" + sessType;
		wx.navigateTo({
			url: pageUrl
		})
	},

	goVoteDetail(e) {
		let data = e.currentTarget.dataset,
			voteStatus = data.votestatus,
			voteId = data.voteId,
			status = data.status;
		if (status == 1) {
			this.showPageMsg("tips", "投票暂未开始，敬请期待！");
		}
		if (status == 2 && voteStatus == 0) {
			// wx.showLoading({
			//   title: '正在加载中...'
			// })
			wx.navigateTo({
				url: '/packageTab1/pages/voteDetail/voteDetail?voteId=' + voteId
			});
		}
		if (status == 2 && voteStatus == 1) {
			wx.showToast({
				title: '您已投票，不能再投了！',
			});
		}
		if (status == 3) {
			wx.showToast({
				title: '投票已结束，请重新选择',
			})
		}

	},
	goVoteEnd(event) {
		var voteId = event.currentTarget.dataset.voteid;
		wx.navigateTo({
			url: '../voteEnd/voteEnd?voteId=' + voteId
		})
	},

	// 页面被拉到底部
	onReachBottom: function (e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		var status = this.data.status;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex,
				"status": status
			});
			this.loadpartyVoteList();
		} else {
			this.showPageMsg("tips", "没有更多内容了");
		}
	},

	// 页面 上拉刷新
	onPullDownRefresh: function (e) {
		this.setData({
			pageIndex: 1,
			contentList: [],
		});

		this.onLoad();
		wx.stopPullDownRefresh();
	},
});